<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

define('MAX_WIDTH', 600);
define('MAX_HEIGHT', 400);
define('MAX_WIDTH_THUMB', 36);
define('IMAGE_REF_WIDTH', 218);
define('IMAGE_REF_HEIGHT', 136);

/**
 * SectionsController
 * 
 * @package JL Panel
 * @author JLQ
 * @copyright 2009
 * @version 1.0
 * @access public
 */
class ImagesController extends JLControllerBase {

    private $config = null;
    private $path = "Images";
    private $title = "Système d'Actualisation CSME > Images";
    private $data = null;
    private $galleryPath = "galeries";

    /**
     * ImagesController::insertImages()
     * 
     * @return
     */
    function insertImages() {
        if ($this->Ajax->isAjaxRequest()) {
            $pathGallery = filter_var($_POST['_pathGallery'], FILTER_SANITIZE_STRING);
            $int_id_gallery = filter_var($_POST['_int_id_gallery'], FILTER_SANITIZE_NUMBER_INT);
            $var_name_image = filter_var($_POST['_nameimage'], FILTER_SANITIZE_STRING);

            require JLPATH_MODELS . DS . "ImagesModel.php";
            require JLPATH_MODELS . DS . "GalleryModel.php";
            $galleries = new GalleryModel();
            $images = new ImagesModel();

            $new_name_image = 'image-' . time() . '.jpg';
            $nameimage = '<img width="36" src="../galeries/' . $pathGallery . '/thumb/' . $new_name_image . '" />';
            $_true = $images->insertImages($int_id_gallery, $new_name_image, $nameimage);

            if ($_true != 0) {
                $gallerie = $galleries->getGalleryById($int_id_gallery);
                $num = intval($gallerie['int_num_photos']) + 1;
                $galleries->updateNumGallery($int_id_gallery, $num);

                $oldPath = JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $pathGallery . DS . "view" . DS . $var_name_image;
                $newPathThumb = JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $pathGallery . DS . "thumb" . DS . $new_name_image;
                $newPath = JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $pathGallery . DS . "view" . DS . $new_name_image;

                require JLPATH_LIBRARIES . DS . 'Tools' . DS . 'JLImage.class.php';

                $image = new JLImage();
                $image->load($oldPath);

                if ($image->getWidth() > MAX_WIDTH) {
                    $image->resizeToWidth(MAX_WIDTH);
                }

                if ($image->getHeight() > MAX_HEIGHT) {
                    $image->resizeToHeight(MAX_HEIGHT);
                }

                $image->save($newPath, 100, FALSE);

                if (empty($gallerie['var_image_reference'])) {
                    $name_ref = $gallerie['var_image_reference'];

                    if (empty($name_ref)) {
                        $name_ref = 'image-' . time() . '.' . $image->getExtension();
                    }

                    $image->centerCrop(IMAGE_REF_WIDTH, IMAGE_REF_HEIGHT);
                    $image->save(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . 'gallery-reference' . DS . $name_ref, 100, FALSE);
                    $galleries->updateImageRefGallery($int_id_gallery, $name_ref);
                }

                $image->centerCrop(MAX_WIDTH_THUMB, MAX_WIDTH_THUMB);
                $image->save($newPathThumb, 70);
                @unlink($oldPath);
                echo $_true;
            } else {
                echo 0;
            }
        } else {
            $this->redirect();
        }
    }

    /**
     * ImagesController::deleteImages()
     * 
     * @return
     */
    function deleteImages() {
        $ids = $_POST['_ids'];
        $pathGallery = $_POST['_pathGallery'];
        $ides = explode(",", $ids);

        require JLPATH_MODELS . DS . "GalleryModel.php";
        require JLPATH_MODELS . DS . "ImagesModel.php";
        $Images = new ImagesModel();
        $galleries = new GalleryModel();

        $i = 0;
        foreach ($ides as $id) {
            $Image = $Images->getImagesById($id);
            $namedir = $Image['var_name_image'];
            $int_id_gallery = $Image['int_id_gallery'];
            $_true = $Images->deleteImages($id);

            $gallerie = $galleries->getGalleryById($int_id_gallery);
            $num = intval($gallerie['int_num_photos']) - 1;
            $galleries->updateNumGallery($int_id_gallery, $num);

            if ($_true) {
                @unlink(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $pathGallery . DS . "view" . DS . $namedir);
                @unlink(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . $pathGallery . DS . "thumb" . DS . $namedir);

                if ($num <= 0) {
                    @unlink(JLPATH_WEB_SITE . DS . $this->galleryPath . DS . 'gallery-reference' . DS . $gallerie['var_image_reference']);
                    $galleries->updateImageRefGallery($int_id_gallery, '');
                }
            } else
                break;

            $i++;
        }

        $nodelete = count($ides) - $i;

        if (!$_true) {
            echo $i . "  Donnée(s) effacer \n$nodelete donnée(s) n'a pu être effacer. Veuillez changer l'état à Provisoire.";
        } else
            echo $i . "  Donnée(s) effacer";
    }

}